package pingan.com.imandtwo.activity.chat;

import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;

import java.util.List;

import pingan.com.imandtwo.R;

/**
 * Created by 迷人的脚毛！！ on 2018/2/7.
 */

public class RecyclerAdpateraa extends  RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<Bean> list;
    private Context context;

    // 多条目的两种类型
    private static final int TYPE_HAS_PIC = 1;
    private static final int TYPE_NO_PIC = 0;

    public RecyclerAdpateraa(List<Bean> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(viewType == 0){
            //第一种
            View view = View.inflate(context, R.layout.liaotian, null);
            ViewHolder holder = new ViewHolder(view);
            return holder;
        }else if(viewType == 1){
            //第二种
            View view = View.inflate(context, R.layout.liaotian2, null);
            ViewHolder1 holder1 = new ViewHolder1(view);
            return holder1;
        }else{
            View view = View.inflate(context, R.layout.liaotian, null);
            ViewHolder holder = new ViewHolder(view);
            return holder;
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof ViewHolder){
            ViewHolder h = (ViewHolder) holder;
            h.txtTitle.setText(list.get(position).getText());
            Uri uri = Uri.parse("http://img4.imgtn.bdimg.com/it/u=480194109,2955193021&fm=27&gp=0.jpg");

            h.ima.setImageURI(uri);
        }else if(holder instanceof ViewHolder1){
            ViewHolder1 h1 = (ViewHolder1) holder;
            h1.txtTitle.setText(list.get(position).getText());
            Uri uri = Uri.parse("http://img2.imgtn.bdimg.com/it/u=4245854822,3110644641&fm=214&gp=0.jpg");

            h1.ima.setImageURI(uri);
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (list.get(position).getType().equals("0")) {
            return TYPE_HAS_PIC;
        } else {
            return TYPE_NO_PIC;
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        private TextView txtTitle;
        private SimpleDraweeView ima;
        public ViewHolder(View itemView) {
            super(itemView);
            txtTitle = (TextView) itemView.findViewById(R.id.txtTitle);
            ima = (SimpleDraweeView) itemView.findViewById(R.id.ima);
        }
    }

    class ViewHolder1 extends RecyclerView.ViewHolder{
        private TextView txtTitle;
        private SimpleDraweeView ima;
        public ViewHolder1(View itemView) {
            super(itemView);
            txtTitle = (TextView) itemView.findViewById(R.id.txtTitleaa);
            ima = (SimpleDraweeView) itemView.findViewById(R.id.ima);
        }
    }
}
